import { message } from 'antd';
import api from '../../api';
export default {
  namespace: 'tenantManageM',
  state: {
    data: [],
    loading: true,
    sysOrgan: [], //下游租户数据
    sceneList: [],
    templateList: [],
  },
  effects: {
    // 编辑
    *sysUpdate({ payload, callback }, { call, put }) {
      const data = yield call(api.tenantManage.sysUpdate, payload);
      if (data && data.code == 0) {
        callback('success');
        message.success('修改成功');
      } else {
        callback('fail');
        message.error(`编辑失败，失败原因：${data?.msg}`);
        console.error(`编辑失败，失败原因：${data?.msg}`, '接口名称： update 接口：portfolio-server/tenant/update');
      }
    },
    // 下游租户
    *sysOrgan({ payload }, { call, put }) {
      const data = yield call(api.tenantManage.sysOrgan, payload);
      if (data && data.code == 0) {
        let tempArr = [];
        data.data.dataSource &&
          data.data.dataSource.forEach((item) => {
            tempArr.push({ title: item.organName, value: item.organCode });
          });
        yield put({
          type: 'save',
          payload: {
            sysOrgan: tempArr,
          },
        });
      } else {
        message.error(`获取下游租户失败，失败原因：${data?.msg}`);
        console.error(`获取下游租户失败，失败原因：${data?.msg}`, '接口名称： sys-organ 接口：portfolio-server/tenant/sys-organ');
      }
    },
    // 查询
    *Search({ payload }, { call, put }) {
      const data = yield call(api.tenantManage.Search, payload);
      if (data && data.code == 0) {
        yield put({
          type: 'save',
          payload: {
            data: data.data.dataSource,
            loading: false,
          },
        });
      } else {
        message.error(`查询租户列表失败，失败原因：${data?.mesg}`);
        console.error(`查询租户列表失败，失败原因：${data?.mesg}`, '接口名称： page-query 接口：portfolio-server/tenant/page-query');
      }
    },
    // 删除
    *deleteUser({ payload, callback }, { call, put, select }) {
      const data = yield call(api.tenantManage.deleteUser, payload);
      if (data && data.code == 0) {
        callback('success');
        message.success('删除成功');
      } else {
        callback('fail');
        message.error(`删除失败，失败原因：${data?.msg}`);
        console.error(`删除失败，失败原因：${data?.msg}`, '接口名称： 删除的id名 接口：portfolio-server/tenant/delete/${query}');
      }
    },
    // 新增
    *saveTable({ payload, callback }, { call, put, select }) {
      const data = yield call(api.tenantManage.saveTable, payload);
      if (data && data.code == 0) {
        callback('success');
        message.success('操作成功');
      } else {
        callback('fail');
        message.error(`保存失败，失败原因：${data?.msg}`);
        console.error(`保存失败，失败原因：${data?.msg}`, '接口名称： save 接口：portfolio-server/tenant/save');
      }
    },
    // 获取用户表格数据
    *SearchUserData({ payload, callback }, { call, put }) {
      const data = yield call(api.getUserList, payload);
      if (data && data.success) {
        callback('success', data.data);
      } else {
        callback('fail');
        message.error(`获取账户下拉数据失败，失败原因：${data?.mesg}`);
        console.error(`获取账户下拉数据失败，失败原因：${data?.mesg}`, '接口名称： findAll 接口：portfolio-server/user/findAll');
      }
    },
    // 获取测试报告情景 下拉列表
    *getScenes({ payload }, { call, put }) {
      const res = yield call(api.sceneSettings.getScenes, payload);
      if (res && res?.code === 0) {
        let tempArr = (res.data || []).map((item) => {
          return { value: item.value + '', title: item.display };
        });
        yield put({
          type: 'save',
          payload: {
            sceneList: tempArr,
          },
        });
      }
    },
    // 获取测试报告情景 下拉列表
    *getTemplate({ payload }, { call, put }) {
      const res = yield call(api.sceneSettings.getModels, {
        userName: 'default_maat_user_in_evar',
        orgCode: 'ZCTGB',
        systemCode: 'maat',
      });
      if (res && res?.code === 0) {
        let tempArr = (res.data || []).map((item) => {
          return { value: item.ID + '', title: item.C_TEMPLATE_NAME };
        });
        yield put({
          type: 'save',
          payload: {
            templateList: tempArr,
          },
        });
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
